package com.irdstudio.apicenter.console.controller.appframe;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.dm.AppFrameCertificate;
import com.irdstudio.apicenter.console.service.appframe.AppFrameCertificateService;

import javax.servlet.http.HttpServletRequest;

/**
 * 平台模块管理
 * 
 * @author jr
 * 
 */
@Controller
@RequestMapping(value = "/appFrameCertificate")
public class AppFrameCertificateAction {
	@Autowired
	private AppFrameCertificateService appFrameCertificateService;

	//
	// /*
	// * 查询平台模块信息057156686510
	// */
	// @RequestMapping(value = "modulelist")
	// public ModelAndView moduleList(HttpServletRequest request) throws
	// Exception {
	// // 获取页码和条数service
	// // int page = Integer.parseInt(request.getParameter("page"));// 当前第几页
	// // int row = Integer.parseInt(request.getParameter("rows"));// 每页显示多少条
	// String id="";
	// String name="";
	// if(request.getParameter("moduleid")!=null)
	// {
	//			 
	// name = request.getParameter("modulename");// 模块编码
	// }
	// if(request.getParameter("moduleid")!=null)
	// {
	//			 
	// name = request.getParameter("modulename");// 模块编码
	// }
	//		
	// ModelAndView modelAndView = new ModelAndView();
	// List<AppFrameStart> modulelist = appFrameStartService.queryModule(id);
	// modelAndView.addObject("modulelist", modulelist);
	// modelAndView.setViewName("admin/moduleList");
	// return modelAndView;
	// }

	@RequestMapping(value = "appFrameCertificateDetail1")
	public ModelAndView queryWithKeys(HttpServletRequest request)
			throws Exception {
		String appid = request.getParameter("appid");
		ModelAndView modelAndView = new ModelAndView();
		AppFrameCertificate appFrameCertificate = appFrameCertificateService
				.queryAppFrameStartDetail(appid);
		appFrameCertificate.setAppId(appid);
		modelAndView.addObject("appFrameCertificate", appFrameCertificate);
		modelAndView.setViewName("appframe/certificate");
		return modelAndView;
	}

	@RequestMapping(value = "updateAppFrameCertificate")
	public void updateAppFrameStart(
			@ModelAttribute("appFrameCertificate") AppFrameCertificate appFrameCertificate,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		int flag = -1;
		AppFrameCertificate appFrameCertificate1 = appFrameCertificateService
				.queryAppFrameStartDetail(appFrameCertificate.getAppId());
		if (appFrameCertificate1.getAppId() == null
				|| "".endsWith(appFrameCertificate1.getAppId())) {
			flag = appFrameCertificateService.insertOne(appFrameCertificate);
		} else {
			flag = appFrameCertificateService.updateByKeys(appFrameCertificate);
		}

		// int flag =
		// appFrameCertificateService.updateByKeys(appFrameCertificate);
		/*
		 * modelAndView.addObject("appFrameCertificate", appFrameCertificate);
		 * modelAndView.addObject("result",flag);
		 * modelAndView.setViewName("appframe/certificate"); return
		 * modelAndView;
		 */

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	/*
	 * 查询模块详细信息 输出json
	 */
	@RequestMapping(value = "appFrameStartDetail")
	public @ResponseBody
	AppFrameCertificate queryAppFrameStartDetail(Model model,
			@PathVariable("id") String appid) throws SQLException,
			ClassNotFoundException {
		AppFrameCertificate appFrameCertificate = appFrameCertificateService
				.queryAppFrameStartDetail("1");
		return appFrameCertificate;
	}

}
